package com.bwie.bwedu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.activity.MainActivity;
import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.utils.LogUtils;
import com.bwie.bwedu.utils.PhoneUtil;

import java.util.List;
/**
 * Created by zhang on 2016/7/22.
 */
public class LocalSaveAdapter extends MyBaseAdapter<Student>{
    private Context context;
    private ViewHolder holder;
    ItemClickListener itemClickListener;

    public  void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
    //定义一个接口
    public interface  ItemClickListener{
        //上传
        void onInvalidClick(int position);
        //编辑
        void onValidClick(int position);
        //删除
        void deleteClick(int position);
    }
    public LocalSaveAdapter(Context context, List<Student> list) {
        super(context, list);
        LogUtils.d(list.size()+"----------------------WaittingConfirmFragmentAdapter-------------------");
        this.context=context;
    }
    @Override
    public View myView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = View.inflate(context, R.layout.fragment_local_item,null);
            holder = new ViewHolder();
            holder.name= (TextView) convertView.findViewById(R.id.track_item_name);
            holder.phone= (TextView) convertView.findViewById(R.id.track_item_phone);
            holder.z_name= (TextView) convertView.findViewById(R.id.track_item_z_name);
            holder.z_phone= (TextView) convertView.findViewById(R.id.track_item_z_phone);
            holder.address= (TextView) convertView.findViewById(R.id.track_item_address);
            holder.id_card= (TextView) convertView.findViewById(R.id.track_item_ID_card);
            holder.uplode=(Button)convertView.findViewById(R.id.upload);
            holder.edit=(Button)convertView.findViewById(R.id.edit);
            holder.delete=(ImageView)convertView.findViewById(R.id.delete);

            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder) convertView.getTag();
        }

        setListener(position,convertView);

        LogUtils.e("==========="+position);
        if ("2".equals(getList().get(position))){
            holder.uplode.setVisibility(View.GONE);
            holder.edit.setVisibility(View.GONE);
            holder.delete.setVisibility(View.GONE);
        }else{
            holder.uplode.setVisibility(View.VISIBLE);
            holder.edit.setVisibility(View.VISIBLE);
            holder.delete.setVisibility(View.VISIBLE);
        }
        holder.name.setText(getList().get(position).gxxm);
        holder.phone.setText(getList().get(position).gxdh);
        holder.z_name.setText(getList().get(position).xm);
        holder.z_phone.setText(getList().get(position).grsj);
        holder.address.setText(getList().get(position).jg);
        holder.id_card.setText(getList().get(position).sfzh);
//        public String grsj;//"学员手机号;
//        public String jg;// : "学员籍贯";
//        public String sfzh;// : "学员身份证号";
//        public String gxxm;//: 联系人";
//        public String gxdh;// : 联系人手机号
        //设置监听---------------

        return convertView;
    }

    public class  ViewHolder{
        TextView name;
        TextView phone;
        TextView z_name;
        TextView z_phone;
        TextView address;
        TextView id_card;
        TextView date;
        Button uplode;
        Button edit;
        LinearLayout layout;
        ImageView delete;
    }
    public void setListener(final int position,final View convertView){
        //TODO 更改布局，删除接口
        //为上传设置监听
        holder.uplode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建对话框
                //对取消按钮设置监听
                //对确定按钮设置监听，网络请求
                itemClickListener.onInvalidClick(position);
            }
        });
        //为编辑设置监听
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onValidClick(position);
            }
        });
        //删除按钮设置监听
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.deleteClick(position);
            }
        });
        //对联系人手机号设置监听
        holder.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String phone= holder.phone.getText().toString();
                PhoneUtil.phone(getList().get(position).gxdh,(MainActivity)getContext(),convertView);
            }
        });
        //对学员手机号设置监听
        holder.z_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone= holder.z_phone.getText().toString();
                PhoneUtil.phone(phone,(MainActivity)getContext(),convertView);

            }
        });

    }
}
